﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using QuanLyVanBanBUS;

public partial class QLVB_User_PhieuTrinh : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            layDanhSachNoiPhatHanh();
            if (cmbMaNoiPhatHanh.Items.Count > 0)
            {
                cmbMaNoiPhatHanh.SelectedIndex = 0;

                layThongTinChiTietMaNoiPhatHanh();
            }

            layDanhSachPhieuTrinh();
        }
    }

    protected void layDanhSachPhieuTrinh()
    {
        PhieuTrinhBUS ptBUS = new PhieuTrinhBUS();
        gvDanhSachPhieuTrinh.DataSource = ptBUS.layDanhSachPhieuTrinh();
        gvDanhSachPhieuTrinh.DataBind();
    }
    protected void layDanhSachNoiPhatHanh()
    {
        NoiPhatHanhBUS nphBUS = new NoiPhatHanhBUS();

        cmbMaNoiPhatHanh.DataSource = nphBUS.layDanhSachNoiPhatHanh();
        cmbMaNoiPhatHanh.DataTextField = "TenNoiPhatHanh";
        cmbMaNoiPhatHanh.DataValueField = "MaNoiPhatHanh";
        cmbMaNoiPhatHanh.DataBind();
    }
    protected void btnDongY_Click(object sender, EventArgs e)
    {
        //Kiem tra rong
        MyTextBox myTextBox = new MyTextBox();

        MyDropDownList myDropDownList = new MyDropDownList();

        if (myTextBox.kiemtraRong(txtSoThuTu) == true)
        {
            Alert.Show("So thu tu khong duoc rong");

            return;
        }
        
        if (myTextBox.kiemtraRong(txtSoVanBan) == true)
        {
            Alert.Show("So van ban khong duoc rong");

            return;
        }

        if (myTextBox.kiemtraRong(txtTenVanBan) == true)
        {
            Alert.Show("Ten van ban khong duoc rong");

            return;
        }

        if (myTextBox.kiemtraRong(txtNgayHetHieuLuc) == true)
        {
            txtNgayHetHieuLuc.Text = DateTime.Now.Date.ToString();
        }

        if (myTextBox.kiemtraRong(txtNoiDungVanBan) == true)
        {
            Alert.Show("Noi dung van ban khong duoc rong");

            return;
        }

        PhieuTrinh ptDTO = new PhieuTrinh();
        if (txtSoThuTu.Text.Length < txtSoThuTu.MaxLength)
        {
            int soLuong = txtSoThuTu.MaxLength - txtSoThuTu.Text.Length;
            txtSoThuTu.Text = themSo0(soLuong) + txtSoThuTu.Text;
        }
        ptDTO.SoThuTu = txtSoThuTu.Text + DateTime.Today.Year.ToString();
        ptDTO.SoVanBan = txtSoVanBan.Text.ToUpper();
        ptDTO.TenVanBan = txtTenVanBan.Text;
        ptDTO.MaNoiPhatHanh = cmbMaNoiPhatHanh.SelectedValue.ToString();
        ptDTO.TenDonViSoanThao = txtTenDonViSoanThao.Text.ToUpper();
        ptDTO.NgayHetHieuLuc = DateTime.ParseExact(txtNgayHetHieuLuc.Text, "dd/MM/yyyy", null);

        //Them
        PhieuTrinhBUS ptBUS = new PhieuTrinhBUS();
        ptBUS.themPhieuTrinh(ptDTO);

        Alert.Show("Da them thanh cong");

        layDanhSachPhieuTrinh();
    }
    protected void btnDong_Click(object sender, EventArgs e)
    {
        Response.Redirect("SoanThao.aspx");
    }
    protected void gvDanhSachPhieuTrinh_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        PhieuTrinh ptDTO = new PhieuTrinh();
        ptDTO.MaPhieuTrinh = Convert.ToInt32(gvDanhSachPhieuTrinh.DataKeys[e.RowIndex].Value);

        PhieuTrinhBUS ptBUS = new PhieuTrinhBUS();
        ptBUS.xoaPhieuTrinh(ptDTO);

        Alert.Show("Da xoa thanh cong");

        layDanhSachPhieuTrinh();
    }
    protected void gvDanhSachPhieuTrinh_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        int maPhieuTrinh = Convert.ToInt32(gvDanhSachPhieuTrinh.DataKeys[e.NewSelectedIndex].Values["MaPhieuTrinh"].ToString());
        Session["MaPhieuTrinh"] = maPhieuTrinh;

        string soThuTu = gvDanhSachPhieuTrinh.DataKeys[e.NewSelectedIndex].Values["SoThuTu"].ToString();
        Session["SoThuTu"] = soThuTu;

        Response.Redirect("BaoCaoDanhSachPhieuTrinh.aspx");
    }
    protected void gvDanhSachPhieuTrinh_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDanhSachPhieuTrinh.PageIndex = e.NewPageIndex;

        layDanhSachPhieuTrinh();
    }
    protected void cmbMaNoiPhatHanh_SelectedIndexChanged(object sender, EventArgs e)
    {
        layThongTinChiTietMaNoiPhatHanh();
    }
    private void layThongTinChiTietMaNoiPhatHanh()
    {
        NoiPhatHanh nphDTO = new NoiPhatHanh();
        nphDTO.MaNoiPhatHanh = cmbMaNoiPhatHanh.SelectedValue;

        NoiPhatHanhBUS nphBUS = new NoiPhatHanhBUS();

        IList<NoiPhatHanh> lstThongTinNoiPhatHanh = nphBUS.layThongTinNoiPhatHanhTheoMaNoiPhatHanh(nphDTO);
        switch (nphDTO.MaNoiPhatHanh)
        {
            case "BIDV":
                txtTenDonViSoanThao.Text = "BIDV";
                break;
            case "BIDVCM":
                txtTenDonViSoanThao.Text = "BIDV CAMAU";
                break;
            case "PT":
                txtTenDonViSoanThao.Text = "BIDV CAMAU";
                break;
            default:
                txtTenDonViSoanThao.Text = string.Empty;
                break;
        }
    }
    protected string themSo0(int soLuong)
    {
        string temp = string.Empty;
        for (int i = 0; i < soLuong; i++)
        {
            temp += "0";
        }

        return temp;
    }
}